// @ts-nocheck
import router from '@system.router';
import BaseParseConfModel from '../../model/baseParseConfImpl/BaseParseConfModel.js';
import LogUtil from '../../common/baseUtil/LogUtil.js';
import powermgr from '@ohos.power'
import prompt from '@ohos.prompt';

let baseParseConfModel = new BaseParseConfModel();
let logUtil = new LogUtil();
let list = [
    {
        "settingIcon": "/res/image/icon_shutdownDevice.png",
        "settingType": 1,
        "settingAlias": "shutdown",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "rebootDevice",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "reboot",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "wakeupDevice",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "suspendDevice",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "getDeviceMode",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    },
    {
        "settingIcon": "/res/image/icon_rebootDevice.png",
        "settingType": 1,
        "settingAlias": "setDeviceMode",
        "settingTitle": "",
        "settingValue": "",
        "settingArrow": "/res/image/ic_settings_arrow.png",
        "settingShowDivider": true
    }
];

export default {
    data: {
        wakeupTimeout: [3, 5, 10, 20, 30],
        index: 0,
        modeInd: 0,
        modeStr: ["标准模式:600", "省电模式:601", "性能模式:602", "超级省电模式:603"],
        modevalue: "",
        powermgrList: [],
        maxPerf: 2000,
        beginTime: 0,
        endTime: 0,
        powerJsonPath: '/data/app/el1/bundle/public/com.example.powerhap/com.example.entry/assets/power/resources/rawfile/powermgrList.json'
    },
    onInit() {
        this.powermgrList = baseParseConfModel.getJsonData(this.powerJsonPath);
        this.powermgrList = this.powermgrList || list
        logUtil.info('powermgrPage get powermgrList by json:' + JSON.stringify(this.powermgrList))
        for (let key in this.powermgrList) {
            var settingAlias = this.powermgrList[key].settingAlias
            this.powermgrList[key].settingTitle = this.$t('strings.'.concat(settingAlias))
        }
        this.setWakeupTimeout()
        this.getDeviceMode()
        logUtil.info('powermgrPage onInit end:')
    },
    setWakeupTimeout() {
        this.index = (this.index >= this.wakeupTimeout.length) ? 0 :this.index
        this.powermgrList[3].settingValue = "灭屏 " + (this.wakeupTimeout[this.index] + "(m)") + " 自动亮屏"
    },
    getDeviceMode() {
        let mode = this.promptAction(() => {return powermgr.getPowerMode()})
        this.modeInd = mode - powermgr.DevicePowerMode.MODE_NORMAL
        this.powermgrList[5].settingValue = this.modeStr[this.modeInd]
    },
    setDeviceMode({ newValue: newValue, newSelected: newSelected }) {
        let mode = newSelected + 600;
        logUtil.info('SetMode: ' + newValue + " " + newSelected + " " + mode)
        this.promptAction(() => {return powermgr.setPowerMode(mode)});

//        this.promptAction(() => {return powermgr.setPowerMode(mode, (error) => {
//            if (typeof error !== "undefined") {
//                prompt.showToast({
//                    message: 'callback: ' + 'code: ' + error.code + ' msg: ' + error.message,
//                    duration: 3000,
//                });
//            }
//        })});
        this.getDeviceMode()
    },

    onclick_power(e) {
        switch (e.detail.text.settingAlias) {
            case 'shutdown':
                this.promptAction(() => {powermgr.shutdown("power_js_test_shutdown")})
//                this.promptAction(() => {powermgr.shutdown(0)})
                break;
            case 'reboot':
                this.promptAction(() => {powermgr.reboot("power_js_test_reboot")})
//                this.promptAction(() => {powermgr.reboot(0)})
                break;
            case 'rebootDevice':
                powermgr.rebootDevice("power_js_test_reboot_device")
                break;
            case 'wakeupDevice':
                ++this.index
                this.setWakeupTimeout()
                break;
            case 'suspendDevice':
                this.promptAction(() => {powermgr.suspend()})
                setTimeout(() => {
                    this.promptAction(() => {powermgr.wakeup("power_js_wakeup")})
//                    this.promptAction(() => {powermgr.wakeup(123)})
                }, this.wakeupTimeout[this.index] * 1000)
                logUtil.info('wakeupDevice timeout: ' + this.wakeupTimeout[this.index] * 1000)
                break;
            case 'setDeviceMode':
                this.$element("picker_mode").show();
                break;
            default:
                break;
        }
    },

    promptAction (func) {
        let ret = null;
        try {
            ret = func();
            let isPromise = (typeof ret === 'object') && (typeof ret.then === 'function')
            console.log("is isPromise: " + isPromise)
            if (isPromise) {
                ret.then((data) => {
                    console.log("is isPromise data: " + data)
                }).catch((error) => {
                    prompt.showToast({
                        message: 'code: ' + error.code + ' msg: ' + error.message,
                        duration: 3000,
                    });
                })
            }
        } catch (e) {
            prompt.showToast({
                message: 'code: ' + e.code + ' msg: ' + e.message,
                duration: 3000,
            });
        }
        return ret;
    },

    powermgrBack() {
        router.back();
    },
    onBackPress() {
        router.back();
    },
    onCreate() {
    },
    onReady() {
    },
    onShow() {
    },
    onHide() {
    },
    onDestroy() {
    }
}
